import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import ufs.LSP.ClosedConnection;
import ufs.LSP.ConnectionTimeout;
import ufs.LSP.LspClient;
import ufs.LSP.LspParams;

public class Requester extends LspClient {

	public Requester(String host, int port, LspParams params)
			throws ConnectionTimeout, IOException {
		super(host, port, params);

	}

	public void execute(String password, String lower, String upper) throws ClosedConnection, NoSuchAlgorithmException {
		// Envia mensagem para o servidor
		String msgEnviada = "C "+hashSHA1(password)+" "+lower+" "+upper;
		this.write(msgEnviada.getBytes());

		// Recebe resposta do servidor
		byte[] buf = this.read();
		String msgRecebida = new String(buf);
		System.out.println("Resposta: " + msgRecebida);
	}
	
	private static String hashSHA1(String key) throws NoSuchAlgorithmException {
		MessageDigest md = MessageDigest.getInstance("SHA1");
		md.update(key.getBytes());
		return new BigInteger(1, md.digest()).toString(16);
	}

}
